package com.mjsoftking.aopapp.aop.aspect;

import android.util.Log;

import com.mjsoftking.aopapp.aop.anno.RequestLog;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

/**
 *
 */
@Aspect
public class RequestLogAspect {


    /**
     * 方法执行之前
     */
    @Before(value = "BaseAspect.requestLogAnnotation(requestLog)")
    public void before(JoinPoint joinPoint, RequestLog requestLog) {
        Log.e("TAG", "Before");

    }

    /**
     * 环绕
     */
    @Around("BaseAspect.requestLogAnnotation(requestLog)")
    public Object around(final ProceedingJoinPoint joinPoint, final RequestLog requestLog) throws Throwable {
        Log.e("TAG", "Around");
        return joinPoint.proceed();
    }

    /**
     * 方法执行之后
     */
    @After(value = "BaseAspect.requestLogAnnotation(requestLog)")
    public void after(JoinPoint joinPoint, RequestLog requestLog) {
        Log.e("TAG", "After");
    }

    /**
     * 方法执行完之后执行
     */
    @AfterReturning(value = "BaseAspect.requestLogAnnotation(requestLog)", returning = "returnVal")
    public void afterReturning(JoinPoint joinPoint, RequestLog requestLog, Object returnVal) {
        Log.e("TAG", "AfterReturning");
    }

    /**
     * 发生异常，包括前置拦截产生异常都会触发，但是异常不会被处理掉，安卓应用注意避免崩溃
     */
    @AfterThrowing(value = "BaseAspect.requestLogAnnotation(requestLog)", throwing = "ex")
    public void afterThrowing(JoinPoint joinPoint, RequestLog requestLog, Throwable ex) {
        Log.e("TAG", "AfterThrowing");
        ex.printStackTrace();
    }
}
